from datetime import datetime
import json
import os

from text_util import print_red

def get_current_date():
    """获取当前日期字符串"""
    return datetime.now().strftime("%Y-%m-%d")

def write_time_to_file(file_path, file_name=None, file_extension=".md", content=None):
    """将当前时间和指定内容写入文件"""
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    if file_name is None:
        file_name = get_current_date()

    file_name_with_extension = f"{file_name}{file_extension}"
    file_path = os.path.join(file_path, file_name_with_extension)

    with open(file_path, 'a', encoding='utf-8') as file:
        file.write(current_time + '\n\n')

        if content:
            file.write(content + '\n\n\n')
    
    return file_path

def load_data_from_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data

def save_content_to_file(file_path, content):
    with open(file_path, 'a', encoding='utf-8') as file:
        file.write(content)

def save_content(file_path, content):
    try:
        with open(file_path, 'a') as file:
            file.write(content)
        print_red(f'内容已成功保存到文件: {file_path}')
    except Exception as e:
        print(f'保存文件时出现错误: {e}')

def get_all_files(folder_path):
    # 获取文件夹下的所有文件
    files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
    return files

def get_file_path(category, user_input, obsidian_path=None, word_path=None):
    if category == "word":
        return os.path.join(word_path, f'{user_input}.md')
    elif category == "story":
        if obsidian_path:
            return os.path.join(obsidian_path, category, f'{user_input}.md')
    else:
        # Handle other categories as needed
        pass
